// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import EquipEntity from "../../Models/Equips/EquipEntity";
import EquipManager from "../../Models/Equips/EquipManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class EquipItem extends cc.Component {
    @property(cc.Label)
    nameTxt: cc.Label = null;
    @property(cc.Label)
    lvTxt: cc.Label = null;
    @property(cc.ProgressBar)
    expProgressbar: cc.ProgressBar = null;

    // @property(cc.Label)
    // describeTxt: cc.Label = null;
    @property(cc.Label)
    attributeTxt: cc.Label = null;
    @property(cc.Button)
    takeoffBtn: cc.Button = null;
    @property(cc.Button)
    wearBtn: cc.Button = null;
    @property(cc.Toggle)
    bgToggle1: cc.Toggle = null;
    @property(cc.Sprite)
    icon: cc.Sprite = null;
    @property(cc.Sprite)
    qualityIcon: cc.Sprite = null;
    @property(cc.Button)
    recycleBtn: cc.Button = null;
    @property(cc.Label)
    nullTxt: cc.Label = null;
    @property(cc.Label)
    wearTxt: cc.Label = null;

    equipEy: EquipEntity;
    drawItem(ey: EquipEntity) {
        this.equipEy = ey;
        if (this.equipEy != null) {
            if (this.nullTxt != null) {
                this.nullTxt.node.active = false;
            }

            this.bgToggle1.isChecked = true;
            this.nameTxt.string = this.equipEy.equipTp.equipName;
            this.lvTxt.string = "等级" + this.equipEy.level;
            this.expProgressbar.progress = this.equipEy.exp / this.equipEy.upgradeExp;
            // this.describeTxt.string = this.equipEy.equipTp.equipDescribe;
            this.attributeTxt.string = this.equipEy.attributeDes;
            this.takeoffBtn.node.active = this.equipEy.isWear;
            this.wearBtn.node.active = !this.equipEy.isWear;
            if (this.recycleBtn != null) {
                this.recycleBtn.node.active = !this.equipEy.isWear;
            }
            if (this.wearTxt != null) {
                if (this.equipEy.equipTp.equipPart == 0) {
                    this.wearTxt.string = "配备";
                } else {
                    this.wearTxt.string = "戴上";
                }
            }


            cc.resources.load("icons/" + this.equipEy.equipTp.id, cc.SpriteFrame, (error: Error, assets: cc.SpriteFrame) => {
                this.icon.spriteFrame = assets;
            });
            cc.resources.load("qualityIcon/" + this.equipEy.equipTp.quality, cc.SpriteFrame, (error: Error, assets: cc.SpriteFrame) => {
                this.qualityIcon.spriteFrame = assets;
            });
        } else {
            if (this.recycleBtn != null) {
                this.recycleBtn.node.active = false;
            }
            this.bgToggle1.isChecked = false;
            this.nameTxt.string = "";
            this.lvTxt.string = "";
            // this.describeTxt.string = "?";
            this.attributeTxt.string = "";
            if (this.nullTxt != null) {
                this.nullTxt.node.active = true;
            }
            this.wearBtn.node.active = this.takeoffBtn.node.active = false;
            this.icon.spriteFrame = null;
            cc.resources.load("qualityIcon/1", cc.SpriteFrame, (error: Error, assets: cc.SpriteFrame) => {
                this.qualityIcon.spriteFrame = assets;
            });
            this.expProgressbar.progress = 0;
        }

    }

    recycleClick() {
        if (!this.equipEy) {
            return;
        }
        EquipManager.single.requestRecycleEquip(this.equipEy.id);
    }

    takeoffClick() {
        if (!this.equipEy) {
            return;
        }
        EquipManager.single.requestTakeoffEquip(this.equipEy.id);
    }

    wearClick() {
        if (!this.equipEy) {
            return;
        }
        EquipManager.single.requestWearEquip(this.equipEy.id);
    }

}
